# ==============================================================================
# PowerShell Script to Enable Audio, Mic, and Webcam Redirection on Windows Server
# ==============================================================================

Write-Host "Configuring Windows Server for Multimedia Redirection..." -ForegroundColor Cyan

# ------------------------------------------------------------------------------
# 1. Enable and Start Audio Services
# ------------------------------------------------------------------------------
Write-Host "Step 1: Enabling Audio Services..." -ForegroundColor Yellow

$services = @("Audiosrv", "AudioEndpointBuilder")

foreach ($service in $services) {
    Try {
        Set-Service -Name $service -StartupType Automatic
        if ((Get-Service -Name $service).Status -ne 'Running') {
            Start-Service -Name $service
        }
        Write-Host "  [OK] $service is Running and set to Automatic." -ForegroundColor Green
    } Catch {
        Write-Host "  [ERROR] Failed to configure $service." -ForegroundColor Red
    }
}

# ------------------------------------------------------------------------------
# 2. Configure Remote Desktop Registry Settings (Effective immediately on new session)
# ------------------------------------------------------------------------------
Write-Host "Step 2: Configuring RDP Protocol Settings (Registry)..." -ForegroundColor Yellow

$rdpPath = "HKLM:\SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp"

# Helper function to set registry keys safely
function Set-RdpRegistry {
    param ($Name, $Value, $Description)
    Try {
        Set-ItemProperty -Path $rdpPath -Name $Name -Value $Value -Type DWord -ErrorAction Stop
        Write-Host "  [OK] $Description ($Name = $Value)" -ForegroundColor Green
    } Catch {
        Write-Host "  [ERROR] Could not set $Name" -ForegroundColor Red
    }
}

# fDisableAudioCapture: 0 = Allow Mic Recording
Set-RdpRegistry -Name "fDisableAudioCapture" -Value 0 -Description "Enabled Microphone Recording"

# fDisableCam: 0 = Allow Camera Redirection
Set-RdpRegistry -Name "fDisableCam" -Value 0 -Description "Enabled Webcam Redirection"

# fDisableClip: 0 = Allow Clipboard (Usually on, but ensuring it)
Set-RdpRegistry -Name "fDisableClip" -Value 0 -Description "Enabled Clipboard Sync"

# ------------------------------------------------------------------------------
# 3. Configure Group Policy Overrides (Machine Policies)
# ------------------------------------------------------------------------------
Write-Host "Step 3: Configuring Group Policies via Registry..." -ForegroundColor Yellow

$policyPath = "HKLM:\SOFTWARE\Policies\Microsoft\Windows NT\Terminal Services"

# Ensure path exists
if (!(Test-Path $policyPath)) {
    New-Item -Path $policyPath -Force | Out-Null
}

# fNoAudioPlayback: 0 = Allow Audio Playback
Set-ItemProperty -Path $policyPath -Name "fNoAudioPlayback" -Value 0 -Type DWord
Write-Host "  [OK] Allowed Audio Playback Policy" -ForegroundColor Green

# fDisableAudioCapture: 0 = Allow Audio Recording
Set-ItemProperty -Path $policyPath -Name "fDisableAudioCapture" -Value 0 -Type DWord
Write-Host "  [OK] Allowed Audio Recording Policy" -ForegroundColor Green

# DoNotAllowDriveRedirection: 0 = Allow Drive/Device Redirection
Set-ItemProperty -Path $policyPath -Name "fDisablePNPRedir" -Value 0 -Type DWord
Write-Host "  [OK] Allowed Plug and Play Device Redirection" -ForegroundColor Green

# ------------------------------------------------------------------------------
# 4. Enable Microphone Privacy Settings (Windows Server 2019/2022)
# ------------------------------------------------------------------------------
Write-Host "Step 4: Enabling Microphone Privacy Access..." -ForegroundColor Yellow

$privacyPath = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\CapabilityAccessManager\ConsentStore\microphone"
$valName = "Value"
$allow = "Allow"

Try {
    if (!(Test-Path $privacyPath)) { New-Item -Path $privacyPath -Force | Out-Null }
    Set-ItemProperty -Path $privacyPath -Name $valName -Value $allow
    Write-Host "  [OK] Global Microphone Access set to 'Allow'" -ForegroundColor Green
} Catch {
    Write-Host "  [INFO] Could not set global privacy key (OS might be older version)." -ForegroundColor Gray
}

# ------------------------------------------------------------------------------
# 5. Restart Audio Service to Apply Changes
# ------------------------------------------------------------------------------
Write-Host "Step 5: Restarting Audio Service..." -ForegroundColor Yellow
Restart-Service -Name "Audiosrv" -Force
Write-Host "  [OK] Audio Service Restarted." -ForegroundColor Green

Write-Host "=================================================================="
Write-Host "CONFIGURATION COMPLETE" -ForegroundColor Cyan
Write-Host "Please RESTART the Windows Server to ensure all policies take effect." -ForegroundColor Cyan
Write-Host "=================================================================="